import type { ImportDataProps } from '../importData'
import { ref } from 'vue'
import { isPromise } from '@/utils'

export const useDownloadTemplate = (props: ImportDataProps) => {
  const downloadLoading = ref(false)

  const downloadTemplate = () => {
    if (!props.download) return

    downloadLoading.value = true

    const downloadRes = props.download()
    if (isPromise(downloadRes)) {
      downloadRes.finally(() => {
        downloadLoading.value = false
      })
    } else {
      downloadLoading.value = false
    }
  }

  return {
    downloadLoading,
    downloadTemplate
  }
}
